<script>
    import {mapEventBus} from "../../eventbus/map_signal.js";
    import ActionList from "../base/ActionList.svelte";
    import resources_dispatch from "../../scripts/resource/dispatch.js";
    import SaveManager from "../../scripts/save/SaveManager.js";
    import FullPathData from "../../eventbus/FullPathData.js";
    import action from "../../scripts/character/action.js";
    import MapManager from "../../scripts/maps/MapManager.js";

    export let where;

    let mapManager;
    let saveManager;

    $: resource_name = resource.resource
    export let resource = {
        resource: "stone"
    }

    function getCharacterSource() {
        mapManager = new MapManager(window)
        let {row, col} = FullPathData.source
        return mapManager.map_data[row][col].character

    }

    function pick_resource(number) {
        saveManager = new SaveManager(window);
        // 获取资源
        let player_data = saveManager.load_data('play');
        let power = player_data.power
        let resource_db = saveManager.load_data('resource')
        resource_db[power][resource_name] += number
        let character = getCharacterSource()
        mapEventBus.emit('show-toast', {
            message: `【${character.name}】获取${resources_dispatch[resource_name].name}资源 *${number}`,
            type: 'success'
        });
        saveManager.save_data('resource', resource_db)
        mapEventBus.emit('resource-update', Math.random())

        return true
    }

    let items = [{
        text: "采集",
        description: "采集该地区的资源",
        callback() {
            pick_resource(1)
        }
    }, {
        text: "精准采集",
        description: "消耗部分角色魔力采集大量的资源, 并使当前资源单位消失",
        callback() {
            let character = getCharacterSource();
            let character_action = action.init(character)
            if (character_action.lose_mp(10)) {
                // 随机获取3-10个
                let number = Math.floor(Math.random() * 8) + 3
                pick_resource(number)
            } else {
                alert("魔力不足")
            }
            mapManager = new MapManager(window)
            saveManager = new SaveManager(window)
            let {row, col} = where

            mapManager.map_data[row][col].resource = null
            saveManager.save_data('map-data', mapManager.map_data)

            mapEventBus.emit('resource-update', Math.random())
            mapEventBus.emit('map-reload', Math.random())
        },
    }]

</script>


    <div class="characterActionList">
        <b style="display: block;margin: 0px 4px 10px">
            {resources_dispatch[resource.resource].name} ({where.col}, {where.row})
        </b>

        <hr/>
        <div>
            {#each items as item}
                <ActionList {item} {where}/>
            {/each}
        </div>
    </div>

<style lang="less">
  .characterActionList {
    padding: 10px 0;
    box-sizing: border-box;
  }
</style>